package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 结算中间表
 */
@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private StatementMapper statementMapper;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));

    }

    @Override
    public void saveItem(List<StatementItem> list) {
        // 删除集合中最后一条元素,返回最后一条元素中封装的结算单id 和 折扣金额
        StatementItem statementItem = list.remove(list.size() - 1);
        // 判断list当中集合是否有元素
        if (list == null || list.size() == 0){
            throw  new BusinessException("非法操作,没有消费单项无法结算");
        }
        // 把数据保存到中间表中
        BigDecimal discountAmount = statementItem.getItemPrice();
        Long statementId = statementItem.getStatementId();

        //删除旧的关系,插入新关系
        statementMapper.deleteRelation(statementId);

        BigDecimal totalAmount = new BigDecimal(0.00);
        BigDecimal totalCount = new BigDecimal(0.00);

        //定义总金额,总数量
        for (StatementItem item : list) {
            totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
            totalCount = totalCount.add(item.getItemQuantity());
            statementItemMapper.insert(item);
        }

        // 修改结算单的总金额,总数量,折扣金额(带结算单的id过去)
        statementMapper.updateAmount(statementId,totalAmount,totalCount,discountAmount);

    }

    @Override
    public void pay(Long statementId) {
        // 根据id把数据查询出来
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        // 判断状态必须是消费中,不是消费中的话就不能支付,否则抛出异常
        if (!Statement.STATUS_CONSUME.equals(statement.getStatus())){
            throw new BusinessException("非法操作,你还没有消费,不能支付");
        }

        //修改支付状态 支付时间和支付人
        statementMapper.changePayStatus(statementId,Statement.STATUS_PAID, ShiroUtils.getUser().getId());

    }
}














